package valuecomusa.fijibus.eticket;

import android.os.Bundle;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import valuecomusa.fijibus.eticket.shorthaul.R;

public class Account extends SunbeamActivity {

	TabHost tabHost;
	Button btnUpdate;
	Spinner spnCity;
	Spinner spnResCity;
	Spinner spnGender;
	AcctHistoryAdapter histadapter;
	ListView history;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account);
		
		doCommon();
		
		tabHost = (TabHost) findViewById(android.R.id.tabhost);		
		tabHost.setup();
		
		final TabWidget tabWidget = tabHost.getTabWidget();		
		final FrameLayout tabContent = tabHost.getTabContentView();
		
		tabWidget.removeAllViews();
		
		// Ensure that all tab content childs are not visible at startup.		
		for (int index = 0; index < tabContent.getChildCount(); index++) {
			tabContent.getChildAt(index).setVisibility(View.GONE);		
		}
		
		//Details
		TabSpec tabSpec = tabHost.newTabSpec("Details");
		tabSpec.setContent(new TabHost.TabContentFactory() {
			
			@Override
			public View createTabContent(String tag) {
				// TODO Auto-generated method stub
				return createTabDetails();
			}
		});
		tabSpec.setIndicator(createTabView(tabHost.getContext(), "Details"));
		tabHost.addTab(tabSpec);
		
		// History
		tabSpec = tabHost.newTabSpec("History");
		tabSpec.setContent(new TabHost.TabContentFactory() {
			
			@Override
			public View createTabContent(String tag) {
				// TODO Auto-generated method stub
				return createTabHistory();
			}
		});
		tabSpec.setIndicator(createTabView(tabHost.getContext(), "History"));
		tabHost.addTab(tabSpec);
		
		// Change Password
		tabSpec = tabHost.newTabSpec("Password");
		tabSpec.setContent(new TabHost.TabContentFactory() {
			
			@Override
			public View createTabContent(String tag) {
				// TODO Auto-generated method stub
				return createTabPassword();
			}
		});
		tabSpec.setIndicator(createTabView(tabHost.getContext(), "Password"));
		tabHost.addTab(tabSpec);
		
		btnUpdate = (Button) findViewById(R.id.btUpdate);
		
		tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId.equals("Details") || tabId.equals("Password"))
					btnUpdate.setVisibility(View.VISIBLE);
				else
				{
					btnUpdate.setVisibility(View.GONE);
					// history
					
					if(history==null) {
						history=(ListView)findViewById(R.id.listView1);
						View headerView = ((LayoutInflater)Account.this.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.history_item_header, null, false);
						history.addHeaderView(headerView);
				        
						histadapter= new AcctHistoryAdapter(Account.this);
						history.setAdapter(histadapter);
						histadapter.add(new AcctHistoryInfo("Mar 13 2012", "Check in Nadi to Suva", "15.70", "20"));
						histadapter.add(new AcctHistoryInfo("Mar 13 2012", "Check in Suva to Nadi", "15.70", "35.7"));
					}
				}
			}
		});
		
		// Gender
		spnGender =(Spinner)findViewById(R.id.spnGender);
		SunbeamSpinnerAdapter genderAdapter = new SunbeamSpinnerAdapter(Account.this);
		spnGender.setAdapter(genderAdapter);
		genderAdapter.add("Male");
		genderAdapter.add("Female");
		
		// City
		spnCity =(Spinner)findViewById(R.id.spnCity);
		SunbeamSpinnerAdapter cityAdapter = new SunbeamSpinnerAdapter(Account.this);
		spnCity.setAdapter(cityAdapter);
		fillCity(cityAdapter);
		
		// Res City
		spnResCity =(Spinner)findViewById(R.id.spnResidentCity);
		SunbeamSpinnerAdapter rescityAdapter = new SunbeamSpinnerAdapter(Account.this);
		spnResCity.setAdapter(rescityAdapter);
		fillCity(rescityAdapter);
		
		fillData();
	}

	private void fillCity(SunbeamSpinnerAdapter adapter)
	{
		adapter.add("Ba");
		adapter.add("Lautoka");
		adapter.add("Nadi Airport");
		adapter.add("Nadi");
		adapter.add("Sigatoka");
		adapter.add("Navua");
		adapter.add("Suva");
		adapter.add("Nausori");
		adapter.add("Korovou");
		adapter.add("Rakiraki");
		adapter.add("Tavua");
	}
	
	private void fillData()
	{
		EditText tmp=(EditText)findViewById(R.id.edtFirstName);
		tmp.setText("Latia");
		
		tmp=(EditText)findViewById(R.id.edtLastName);
		tmp.setText("Bonawai");
		
		spnGender.setSelection(1);
		
		tmp=(EditText)findViewById(R.id.edtAddress);
		tmp.setText("P.O Box 2440, Government Buildings");
		spnCity.setSelection(6);
		
		tmp=(EditText)findViewById(R.id.edtResidentAddress);
		tmp.setText("11 Normas Place, Kennedy Avenue");
		spnResCity.setSelection(3);
		
		tmp=(EditText)findViewById(R.id.edtPhone);
		tmp.setText("(679)-9904603");
		
		tmp=(EditText)findViewById(R.id.edtEmail);
		tmp.setText("latia@voicenetip.com");
		
		TextView tvtmp=(TextView)findViewById(R.id.tvCustName);
		tvtmp.setText("Litia Bonawai");
		
	}
	
	private View createTabView(final Context context, final String text) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		return view;
	}
	
	private View createTabDetails() {
		View view = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.account_details, null);
		return view;
	}
	
	private View createTabHistory() {
		View view = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.account_history, null);
		return view;
	}
	
	private View createTabPassword() {
		View view = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.account_password, null);
		return view;
	}

}
